package com.fjc.service.Impl;

import com.fjc.dto.MedicineDTO;
import com.fjc.dto.MedicinePageQueryDTO;
import com.fjc.entity.Medicine;
import com.fjc.mapper.MedicineMapper;
import com.fjc.result.PageResult;
import com.fjc.service.MedicineService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class MedicineServiceImpl implements MedicineService {

    @Autowired
    private MedicineMapper medicineMapper;

    /**
     * 新增药品
     * @param medicineDTO
     */
//    @Transactional  // 涉及多张表的数据，为保证数据的一致性，添加事务注解
    public void save(MedicineDTO medicineDTO) {

        Medicine medicine = new Medicine();

        BeanUtils.copyProperties(medicineDTO, medicine);

        // 获取当前用户id
        // TODO: 先让用户id为固定值，之后再进行修改
//        medicine.setUserId(1L);
        // 设置添加药品的默认状态为启用
        medicine.setStatus(1);
        // 向药品表添加一条数据
        medicineMapper.insert(medicine);

    }

    /**
     * 药品分页查询
     * @param medicinePageQueryDTO
     * @return
     */
    public PageResult pageQuery(MedicinePageQueryDTO medicinePageQueryDTO) {
        PageHelper.startPage(medicinePageQueryDTO.getPage(), medicinePageQueryDTO.getPageSize());
        Page<Medicine> page = medicineMapper.pageQuery(medicinePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
